﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ArcannEngine.Entities;

namespace ArcannEngine.Graphics
{
    public class Interface
    {
        MouseState mouseState = Mouse.GetState();
        MouseState tempMouseState = Mouse.GetState();
        //Rectangle mouseRec;

        ModificationsBox modBox;
        Texture2D modificatorTex;

        Texture2D cursor;
        Rectangle cursorRectangle;

        SpriteBatch spriteBatch;
        GraphicsDevice device;
        RocketGame game;

        Rectangle highlight;
        Texture2D highTex;
        int highOffset = 1;

        Rectangle backgroundRight;
        Rectangle extend;
        Rectangle backgroundTop;
        Texture2D backgroundTex;

        Rectangle rocketVisualiser;
        Texture2D rocketTex;

        Rectangle backgroundLeft;
        Texture2D whiteTexture;

        Rectangle startButton, slowMoButton, resetButton;
        string startButtonDesc, slowMoButtonDesc;

        Rectangle fuelBurnin_plus, fuelBurnin_minus;
        string fuelBurnin;
        Rectangle gravity_plus, gravity_minus;
        string gravity;
        Rectangle area_plus, area_minus;
        string area;

        Texture2D buttonTex;

        SpriteFont defaultFont;
        SpriteFont descriptionsFont;

        List<Rectangle> rectanglesToHighlight;

        RocketEntity rocket;

        private bool extendedInfo = false;
        private bool modifyAble = false;

        public Interface(RocketGame game, RocketEntity rocket, SpriteBatch spriteBatch, GraphicsDevice device)
        {
            this.spriteBatch = spriteBatch;
            this.device = device;
            this.game = game;
            this.rocket = rocket;
            Initialize();
        }

        private void Initialize()
        {
            rectanglesToHighlight = new List<Rectangle>();
            fuelBurnin = "Spalanie";
            gravity = "Grawitacja";
            area = "Pp kadluba";


            defaultFont = game.Content.Load<SpriteFont>("Czcionka");
            descriptionsFont = game.Content.Load<SpriteFont>("Descriptions");
            backgroundTop = new Rectangle(0, 0, device.Viewport.Width / 6, 40);
            
            backgroundLeft = new Rectangle(0, device.Viewport.Height / 3, device.Viewport.Width / 12, device.Viewport.Height - device.Viewport.Height / 3);

            rocketVisualiser = new Rectangle(10, device.Viewport.Height - 100, 35, 35);

            startButton = new Rectangle(device.Viewport.Width - device.Viewport.Width / 4 + 20, 60, device.Viewport.Width / 10, 30);
            slowMoButton = new Rectangle(device.Viewport.Width - 20 - device.Viewport.Width / 10, 60, device.Viewport.Width / 10, 30);
            resetButton = new Rectangle(device.Viewport.Width - 20 - device.Viewport.Width / 10, 60, device.Viewport.Width / 10, 30);

            /*
            //ilosc wyrzucanego paliwa
            Vector2 offset1 = defaultFont.MeasureString(fuelBurnin);
            fuelBurnin_minus = new Rectangle(offset + device.Viewport.Width - device.Viewport.Width / 4 + 60, 120, device.Viewport.Width / 40, 30);
            fuelBurnin_plus = new Rectangle(fuelBurnin_minus.X + offset + 20, 120, device.Viewport.Width / 40, 30);
           
             */
            //grawitacja
            Vector2 offset2 = defaultFont.MeasureString(gravity);
            gravity_minus = new Rectangle(fuelBurnin_minus.X, 170, device.Viewport.Width / 40, 30);
            gravity_plus = new Rectangle(fuelBurnin_plus.X, 170, device.Viewport.Width / 40, 30);
            area_minus = new Rectangle(fuelBurnin_minus.X, 220, device.Viewport.Width / 40, 30);
            area_plus = new Rectangle(fuelBurnin_plus.X, 220, device.Viewport.Width / 40, 30);

            modBox = new ModificationsBox(new Vector2(device.Viewport.Width - device.Viewport.Width / 4, 100), 30, 50, descriptionsFont, spriteBatch);

            highlight = new Rectangle(0, 0, 0, 0);
            cursorRectangle = new Rectangle(0, 0, 30, 30);

            AddToHighlight(startButton);
            AddToHighlight(resetButton);
            /*
            AddToHighlight(fuelBurnin_plus);
            AddToHighlight(fuelBurnin_minus);
            AddToHighlight(gravity_plus);
            AddToHighlight(gravity_minus);
            AddToHighlight(area_plus);
            AddToHighlight(area_minus);
            */



            backgroundTex = game.Content.Load<Texture2D>("Textures//grey");
            buttonTex = game.Content.Load<Texture2D>("Textures//buttonTex");
            whiteTexture = game.Content.Load<Texture2D>("Textures//white");
            highTex = game.Content.Load<Texture2D>("Textures//highTex");
            cursor = game.Content.Load<Texture2D>("Textures//kursor");
            rocketTex = game.Content.Load<Texture2D>("Textures//rocket");
            modificatorTex = game.Content.Load<Texture2D>("Textures//blue");

            modBox.Add(new Modificator(whiteTexture, 0, 50, 0.5f, ref rocket.m_total, "Paliwo", "[kg]", 25));
            modBox.Add(new Modificator(whiteTexture, 0, 50, 0.5f, ref World.g, "Grawitacja", "[m/s^2]", 25));
            modBox.Add(new Modificator(whiteTexture, 0, 50, 1.0f, ref rocket.area, "Pp kadluba", "[m^2]", 25));
            modBox.Add(new Modificator(whiteTexture, 0, 50, 1.0f, ref rocket.area, "Gęstość", "[kg/m^3]", 25));
            modBox.Add(new Modificator(modificatorTex, 0, 50, 1.0f, ref rocket.area, "Cd", "[kg/m^3]", 25));
            modBox.Organize();

            foreach (Modificator mod in modBox.Modificators)
            {
                AddToHighlight(mod.plus);
                AddToHighlight(mod.minus);
            }
        }

        public void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(backgroundTex, backgroundTop, Color.Snow);
            spriteBatch.Draw(backgroundTex, backgroundLeft, Color.White);

            drawRightPanel();

            spriteBatch.Draw(highTex, highlight, Color.CadetBlue);
            spriteBatch.Draw(whiteTexture, startButton, Color.DarkBlue);
            spriteBatch.Draw(whiteTexture, resetButton, Color.DarkRed);
            spriteBatch.Draw(rocketTex, rocketVisualiser, Color.White);


             /*
             //wyrzucane paliwo mod
             spriteBatch.DrawString(defaultFont, fuelBurnin, new Vector2(fuelBurnin_minus.X - (float)this.offset - 45, fuelBurnin_minus.Y ), Color.White);
             spriteBatch.Draw(buttonTex, fuelBurnin_plus, Color.White);
             spriteBatch.Draw(buttonTex, fuelBurnin_minus, Color.White);
             spriteBatch.DrawString(defaultFont, rocket.fuelBurnin + "", new Vector2(fuelBurnin_minus.X + (fuelBurnin_plus.X - fuelBurnin_minus.X + fuelBurnin_minus.Width)/2 - defaultFont.MeasureString(rocket.fuelBurnin+"").X/2, fuelBurnin_plus.Y), Color.White);
             //grawitacja mod
             spriteBatch.DrawString(defaultFont, gravity, new Vector2(fuelBurnin_minus.X - (float)this.offset - 45, gravity_minus.Y), Color.White);
             spriteBatch.Draw(buttonTex, gravity_plus, Color.White);
             spriteBatch.Draw(buttonTex, gravity_minus, Color.White);
             spriteBatch.DrawString(defaultFont, World.g + "", new Vector2(gravity_minus.X + (gravity_plus.X - gravity_minus.X + gravity_minus.Width) / 2 - defaultFont.MeasureString(World.g + "").X / 2, gravity_plus.Y), Color.White);
             //*/
             //grawitacja mod
             /*spriteBatch.DrawString(defaultFont, area, new Vector2(fuelBurnin_minus.X - 45, area_minus.Y), Color.White);
             spriteBatch.Draw(buttonTex, area_plus, Color.White);
             spriteBatch.Draw(buttonTex, area_minus, Color.White);
             spriteBatch.DrawString(defaultFont, rocket.area + "", new Vector2(gravity_minus.X + (gravity_plus.X - gravity_minus.X + gravity_minus.Width) / 2 - defaultFont.MeasureString(rocket.area + "").X / 2, area_plus.Y), Color.White);
            */
            //spriteBatch.Draw(backgroundTex, slowMoButton, Color.White);
            string title = "SYMULACJA RAKIETY KOSMICZNEJ";
            Vector2 offset = defaultFont.MeasureString(title);
            spriteBatch.DrawString(defaultFont, title, new Vector2(device.Viewport.Width - device.Viewport.Width / 4 + (device.Viewport.Width / 4 - offset.X) / 2, 15), Color.White);
            spriteBatch.DrawString(defaultFont, startButtonDesc, new Vector2(device.Viewport.Width - device.Viewport.Width / 4 + 65, 62), Color.White);
            //spriteBatch.DrawString(defaultFont, slowMoButtonDesc, new Vector2(device.Viewport.Width - 10 - device.Viewport.Width / 10, 62), Color.White);
            spriteBatch.DrawString(defaultFont, "Reset", new Vector2(device.Viewport.Width - device.Viewport.Width / 10 + 20, 62), Color.White);
            
            
            
            drawInfo(0,0,10,10);
            
            
            
            //spriteBatch.DrawString(defaultFont, "L - slow motion - now [" + (World.slowMo ? "ON" : "OFF") + "]", new Vector2(10, 40), Color.PowderBlue);
            spriteBatch.DrawString(defaultFont, "Wysokosc: " , new Vector2(backgroundLeft.X + 5, backgroundLeft.Y + 5), Color.Chocolate);
            spriteBatch.DrawString(descriptionsFont, ((int)rocket.height).ToString() + " [m]", new Vector2(backgroundLeft.X + 5, backgroundLeft.Y + 35), Color.White);
            spriteBatch.DrawString(descriptionsFont, "_____", new Vector2(backgroundLeft.X + backgroundLeft.Width - 50, device.Viewport.Height - 105), Color.White);
            spriteBatch.DrawString(descriptionsFont, "0 [m]", new Vector2(backgroundLeft.X + backgroundLeft.Width - 45, device.Viewport.Height - 85), Color.White);
            

            spriteBatch.Draw(cursor, cursorRectangle, Color.White);
            spriteBatch.End();
        }

        public void Update(GameTime gameTime)
        {
            //OPISY
            modBox.Modificators[0].what = rocket.top.fuelContainer + rocket.middle.fuelContainer + rocket.bottom.fuelContainer;
            modBox.Modificators[1].what = World.g;
            modBox.Modificators[2].what = rocket.area;
            modBox.Modificators[3].what = World.rho;
            modBox.Modificators[4].what = World.cd;

            modBox.Organize();
            rocketVisualiser.Y = device.Viewport.Height - 125 - (int)(rocket.height * 0.01f);
            //Console.WriteLine(World.g);
            startButtonDesc = (World.stopped ? "Start" : "Stop");
            slowMoButtonDesc = (World.slowMo ? "T_Off" : "T_On");
            slowMoButtonDesc += " SlowMo";
            tempMouseState = mouseState;

            mouseState = Mouse.GetState();
            Rectangle mouseRec = new Rectangle(mouseState.X, mouseState.Y, 1, 1);
            cursorRectangle.X = mouseRec.X - 8;
            cursorRectangle.Y = mouseRec.Y - 5;

            highlight = new Rectangle(0, 0, 0, 0);

            //Rozszerzone info
            Rectangle dots = new Rectangle(backgroundTop.X + backgroundTop.Width - 30, backgroundTop.Y + backgroundTop.Height - 30, 30, 30);

            foreach (Rectangle rec in rectanglesToHighlight) //podświetlenie, jezeli myszka najechala na jakis button
            {
                if (mouseRec.Intersects(rec))
                {
                    if (rec.Width < 50 && modifyAble == false)
                    {
                    }
                    else
                    {
                        highlight.X = rec.X - highOffset;
                        highlight.Y = rec.Y - highOffset;
                        highlight.Width = rec.Width + highOffset * 2;
                        highlight.Height = rec.Height + highOffset * 2;
                    }
                    break;
                }
            }



            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(startButton))
            {
                World.stopped = !World.stopped;
            }

            if (MousePressed() == true)
            {
                for (int i = 0; i < modBox.Modificators.Count; i++)
                {
                    switch (i)
                    {
                        case 0:
                        {
                            if (mouseRec.Intersects(modBox.Modificators[0].minus))
                            {
                                if (rocket.top.fuelContainer + rocket.middle.fuelContainer + rocket.bottom.fuelContainer > 0)
                                {
                                    rocket.top.fuelContainer *= 0.9f;
                                    rocket.middle.fuelContainer *= 0.9f;
                                    rocket.bottom.fuelContainer *= 0.9f;
                                }
                            }
                            if (mouseRec.Intersects(modBox.Modificators[0].plus))
                            {
                                if (rocket.top.fuelContainer + rocket.middle.fuelContainer + rocket.bottom.fuelContainer < 3500000)
                                {
                                    rocket.top.fuelContainer *= 1.1f;
                                    rocket.middle.fuelContainer *= 1.1f;
                                    rocket.bottom.fuelContainer *= 1.1f;
                                }
                            }

                            modBox.Modificators[0].what = rocket.top.fuelContainer + rocket.middle.fuelContainer + rocket.bottom.fuelContainer;
                            break;
                        }
                        case 1:
                        {
                            if (mouseRec.Intersects(modBox.Modificators[1].minus))
                            {
                                if (World.g - 0.5f > 0)
                                {
                                    World.g -= 0.5f;
                                }
                            }
                            if (mouseRec.Intersects(modBox.Modificators[1].plus))
                            {
                                if (World.g + 0.5f < 25)
                                {
                                    World.g += 0.5f;
                                }
                            }

                            modBox.Modificators[1].what = World.g;
                            break;
                        }
                        case 2:
                        {
                            if (mouseRec.Intersects(modBox.Modificators[2].minus))
                            {
                                if (rocket.area - 0.5f > 0)
                                {
                                    rocket.area -= 0.5f;
                                }
                            }
                            if (mouseRec.Intersects(modBox.Modificators[2].plus))
                            {
                                if (rocket.area + 0.5f < 25)
                                {
                                    rocket.area += 0.5f;
                                }
                            }

                            modBox.Modificators[2].what = rocket.area;
                            break;
                        }
                        case 3:
                        {
                            if (mouseRec.Intersects(modBox.Modificators[3].minus))
                            {
                                if (World.rho - 0.1f > 0)
                                {
                                    World.rho -= 0.1f;
                                }
                            }
                            if (mouseRec.Intersects(modBox.Modificators[3].plus))
                            {
                                if (World.rho + 0.1f < 10)
                                {
                                    World.rho += 0.1f;
                                }
                            }

                            modBox.Modificators[3].what = World.rho;
                            break;
                        }
                        case 4:
                        {
                            if (mouseRec.Intersects(modBox.Modificators[4].minus))
                            {
                                if (World.cd - 0.1f > 0)
                                {
                                    World.cd -= 0.1f;
                                }
                            }
                            if (mouseRec.Intersects(modBox.Modificators[4].plus))
                            {
                                if (World.cd + 0.1f < 1)
                                {
                                    World.cd += 0.1f;
                                }
                            }

                            modBox.Modificators[4].what = World.rho;
                            break;
                        }
                        default: break;
                    }
                }
                foreach (Modificator mod in modBox.Modificators)
                {

                   /* if (mouseRec.Intersects(mod.plus))
                    {
                        mod.Inc();
                        modBox.Organize();
                    }
                    if (mouseRec.Intersects(mod.minus))
                    {
                        mod.Dec();
                        modBox.Organize();
                    }
               */
                }
                if (mouseRec.Intersects(dots))
                {
                    extendedInfo = !extendedInfo;
                }
                if (mouseRec.Intersects(extend))
                {
                    modifyAble = !modifyAble;
                }
            }
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(fuelBurnin_plus))
            {
                
            }
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(fuelBurnin_minus))
            {
            }
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(gravity_plus))
            {
                float mod = 0.2f;
                if (World.g + mod <= 15)
                {
                    World.g += mod;
                }

            }
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(gravity_minus))
            {
                float mod = 0.2f;
                if (World.g - mod > 0)
                {
                    World.g -= mod;
                }
            }
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(area_plus))
            {
                float mod = 1;
                if (rocket.area + mod <= 30)
                {
                    rocket.area += mod;
                }

            }
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(area_minus))
            {
                float mod = 1;
                if (rocket.area - mod > 0)
                {
                    rocket.area -= mod;
                }
            }


            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released && mouseRec.Intersects(slowMoButton))
            {
                //World.slowMo = !World.slowMo;
                World.resetSignal = true;
            }


        }

        public void DrawString(StringObject strObj)
        {
        }

        private bool MousePressed()
        {
            bool pressed = false;
            if (mouseState.LeftButton == ButtonState.Pressed && tempMouseState.LeftButton == ButtonState.Released)
            {
                pressed = true;
            }
            return pressed;
        }

        public void AddToHighlight(Rectangle rec)
        {
            rectanglesToHighlight.Add(rec);
        }

        private void drawInfo(int x, int y, int xOffset, int yOffset)
        {
            int currentY = y + yOffset;
            int currentX = x + xOffset;
            spriteBatch.DrawString(defaultFont, "Informacje: ", new Vector2(currentX, currentY), Color.Chocolate);
            if (extendedInfo == false)
            {
                backgroundTop = new Rectangle(x, y, device.Viewport.Width / 6, 80);
                currentY += yOffset * 4;
                spriteBatch.DrawString(descriptionsFont, "Masa: " + rocket.m_total + "[kg]", new Vector2(currentX, currentY), Color.White);
            }
            else
            {
                backgroundTop = new Rectangle(0, 0, device.Viewport.Width / 6, 230);
                currentY = move(currentY, 40);
                spriteBatch.DrawString(descriptionsFont, "Masa: " + rocket.m_total + "[kg]", new Vector2(currentX, currentY), Color.White);
                currentY = move(currentY, 30);
                spriteBatch.DrawString(descriptionsFont, "Prędkość: " + rocket.v_rocket + "[m/s]", new Vector2(currentX, currentY), Color.White);
                currentY = move(currentY, 30);
                spriteBatch.DrawString(descriptionsFont, "Przysp: " + (rocket.acceleration/10) + "[m/s^2]", new Vector2(currentX, currentY), Color.White);
                currentY = move(currentY, 30);
                spriteBatch.DrawString(descriptionsFont, "Paliwo: " + (rocket.top.fuelContainer + rocket.bottom.fuelContainer + rocket.middle.fuelContainer) + "[kg]", new Vector2(currentX, currentY), Color.White);
                currentY = move(currentY, 30);
                spriteBatch.DrawString(descriptionsFont, "Wysokość: " + rocket.height + "[m]", new Vector2(currentX, currentY), Color.White);
                currentY = move(currentY, 30);
                spriteBatch.DrawString(descriptionsFont, "Czas: " + rocket.time + "[s]", new Vector2(currentX, currentY), Color.White);
            }
            spriteBatch.DrawString(defaultFont, "...", new Vector2(backgroundTop.X + backgroundTop.Width - 25, backgroundTop.Y + backgroundTop.Height - 30), Color.Chocolate);
        }

        private void drawRightPanel()
        {
            spriteBatch.Draw(backgroundTex, backgroundRight, Color.White);
            if (modifyAble == false)
            {
                backgroundRight = new Rectangle(device.Viewport.Width - device.Viewport.Width / 4, 0, device.Viewport.Width / 4, device.Viewport.Height / 5);
            }
            else
            {
                backgroundRight = new Rectangle(device.Viewport.Width - device.Viewport.Width / 4, 0, device.Viewport.Width / 4, device.Viewport.Height / 6 + modBox.Modificators.Count * 55 + 20);
                modBox.Draw();
            }
            
            spriteBatch.DrawString(defaultFont, "...", new Vector2(backgroundRight.X + backgroundRight.Width / 2 - 4, backgroundRight.Y + backgroundRight.Height - 30), Color.White);
            extend = new Rectangle(backgroundRight.X + backgroundRight.Width / 2 - 8, backgroundRight.Y + backgroundRight.Height - 30, 30, 30);
        }

        private int move(int y, int yOffset)
        {
            return y + yOffset;
        }

    }
}
